package xx.iverson.listening_manager.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import xx.iverson.listening_manager.entity.SysUser;
import xx.iverson.listening_manager.mapper.SysUserMapper;
import xx.iverson.listening_manager.service.SysUserService;

import javax.annotation.Resource;

/**
 * xx.iverson.listening_manager.service.impl.SysUserServiceImpl
 * 描述：
 * on 2025/10/16-下午4:42
 *
 * @author weijx
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public String selectTitle() {
        return sysUserMapper.selectTitle();
    }

    @Override
    public boolean updateTitle(String title) {
        return sysUserMapper.updateTitle(title)>0;
    }

    @Override
    public SysUser selectByUsername(String username) {
        return sysUserMapper.selectByUsername(username);
    }

    @Override
    public boolean updatePassword(String oldPassword, String newPassword) {
        // 获取用户
        SysUser user = sysUserMapper.selectByUsername("admin");
        // 验证旧密码
        oldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if (user == null || !(oldPassword.equals(user.getPassword()))) {
            return false;
        }
        // 更新密码
        String encodedNewPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        return sysUserMapper.updatePassword(encodedNewPassword) > 0;
    }
}
